﻿package  
{
	import com.fox2d.events.GameKeyEvent;
	import com.fox2d.Hero2D;
	import com.fox2d.KeyboardGame;
	import com.fox2d.Object2D;
	
	/**
	 * ...
	 * @author Orlando Leite
	 */
	public class MyHero extends Hero2D
	{
		[Embed(source='../lib/lib.swf', symbol='hero_up')]
		private const heroAniNorth:Class;
		
		[Embed(source='../lib/lib.swf', symbol='hero_down')]
		private const heroAniSouth:Class;
		
		[Embed(source='../lib/lib.swf', symbol='hero_left')]
		private const heroAniWest:Class;
		
		[Embed(source='../lib/lib.swf', symbol='hero_right')]
		private const heroAniEast:Class;
		
		private var firing:Boolean = false;
		private var firePerFrame:int = 6;
		private var curFireFrame:int = 0;
		
		public function MyHero() 
		{
			super();
			
			addAnimation( MOVE_NORTH, new heroAniNorth() );
			addAnimation( MOVE_SOUTH, new heroAniSouth() );
			addAnimation( MOVE_WEST, new heroAniWest() );
			addAnimation( MOVE_EAST, new heroAniEast() );
			
			moveSouth();
			stopMove();
			
			addButton( KeyboardGame.BTN_A, fire );
		}
		
		private function fire():void
		{
			firing = true;
			curFireFrame = 0;
		}
		
		private function stopfire():void
		{
			firing = false;
		}
		
		override public function hit(object:Object2D):Boolean 
		{
			return true;
		}
		
		override protected function buttonUpHandle( event:GameKeyEvent ):void 
		{
			if ( event.btn == KeyboardGame.BTN_A )
			{
				stopfire();
			}
			
			super.buttonUpHandle(event);
		}
		
		private function shoot():void
		{
			switch( direction )
			{
				case 1:
					game.addLiveObject( new Bullet( x + 5, y - 22, 0, -15 ) );
					break;
				
				case 2:
					game.addLiveObject( new Bullet( x - 6, y - 15, 0, 15 ) );
					break;
				
				case 3:
					game.addLiveObject( new Bullet( x - 10, y - 25, -15, 0 ) );
					break;
				
				case 4:
					game.addLiveObject( new Bullet( x + 7, y - 22, 15, 0 ) );
					break;
			}
		}
		
		override public function update():void 
		{
			if ( firing )
			{
				if ( curFireFrame == 0 )
				{
					shoot();
				}
				else if ( curFireFrame == firePerFrame )
				{
					curFireFrame = 0;
				}
				else
				{
					curFireFrame++;
				}
			}
			
			super.update();
		}
		
	}
	
}